<?php
require_once ('../configs/yikuaiqu_config.php');
require_once ('../functions/base.php');
require_once ('../functions/functions.user.php');
require_once ('../functions/functions.yikuaiqu.php');
$page = 0 + $_REQUEST["page"];
$pageSize = 0 + $_REQUEST["length"];
$sceneryID = 0 + $_REQUEST["scenery_id"];
$sceneryContentID = 0 + $_REQUEST["content_id"];
if($pageSize == 0){
	$pageSize = 10;
}
$page = $page == 0 ? 1 : $page;

$userModel = get_user_info();
$mysqli = db_connect();
if($sceneryID > 0){
	

	$stmt = $mysqli->stmt_init();
	$stmt->prepare("SELECT fdName,fdContentID FROM tysny.tbZone WHERE id = ?") or die("prepare get sence sql error.");
	$stmt->bind_param("i", $sceneryID);
	$stmt->bind_result($sceneryName, $sceneryContentID);
	$stmt->execute();
	$stmt->fetch();
	$stmt->close();
}
if($sceneryContentID > 0){
	$select = "id,fdUserID,fdText,fdCreate,";
	$select .= "(SELECT fdLogin FROM tysvc.tbUser WHERE tbUser.id = tbComment.fdUserID) fdUserLogin,";
	$select .= "(SELECT fdName FROM tysvc.tbUser WHERE tbUser.id = tbComment.fdUserID) fdUserName,";
	$select .= "(SELECT fdPhone FROM tysvc.tbPhone WHERE tbPhone.fdUserID = tbComment.fdUserID AND tbPhone.fdDefault = 1 AND tbPhone.fdVerified > 0) fdUserPhone,";
	$select .= "(SELECT AVG(tbRank.fdRank) FROM tysvc.tbRank WHERE tbRank.fdContentID = tbComment.fdContentID AND tbRank.fdUserID = tbComment.fdUserID) as fdRank,";
	$select .= "(SELECT COUNT(*) FROM tysvc.tbComment tbLike WHERE tbLike.fdParentID = tbComment.id AND tbLike.fdType = 2) fdLike ";
	$user_id = 0 + $userModel["user_id"];
	if($user_id > 0){
		$select .= ",(SELECT COUNT(*) FROM tysvc.tbComment tbLike2 WHERE tbLike2.fdParentID = tbComment.id AND tbLike2.fdType = 2 AND fdUserID = ".$user_id.") fdLike2 ";
	}
	$stmt = $mysqli->stmt_init();
	$sql = "SELECT SQL_CALC_FOUND_ROWS ".$select
	."FROM tysvc.tbComment WHERE tbComment.fdType = 0 AND tbComment.fdContentID = ? "
	."ORDER BY tbComment.fdCreate DESC "
	."LIMIT ".($page-1)*$pageSize.",{$pageSize}";
	$stmt->prepare($sql) or die("prepare get sence sql error.");
	$stmt->bind_param("i", $sceneryContentID);
	if($user_id > 0){
		$stmt->bind_result($id, $userID, $text, $createDate, $userLogin, $userName, $userPhone, $rank, $like, $like2);
	}else{
		$stmt->bind_result($id, $userID, $text, $createDate, $userLogin, $userName, $userPhone, $rank, $like);
	}
	$stmt->execute();
	$i = 0;
	$rule = "";
	while($stmt->fetch()){
		$comment_id_r[$i] = $id;
		$comment_user_id_r[$i] = $userID;
		$comment_text_r[$i] = $text;
		$comment_create_date_r[$i] = $createDate;
		
		if($userName == ""){
			$userName = $userLogin;
		}
		if($userName == ""){
			$userName = $userPhone;
		}
		
		if(check_email_regex($userName)){
			$userName = substr($userName, 0, strpos($userName, '@'));
		}
		if(check_phone_regex($userName)){
			for($j=3;$j<7;$j++){
				$userName[$j] = '*';
			}
		}
		$comment_user_name_r[$i] = $userName;
		$rank = intval($rank);
		$comment_rank_r[$i] = $rank;
		$comment_rank_string_r[$i] = $rank_mode[$rank];
		$comment_like_r[$i] = $like;
		$comment_current_user_like_r[$i] = 0 + $like2;//当前用户是否有喜欢过
		
		++$i;
	}
	$stmt->close();
	$total_count_row = $mysqli->query('SELECT FOUND_ROWS()')->fetch_row();
	$total_count = $total_count_row[0];
	

	for($i=0;$i<count($comment_id_r);$i++){
		$item = "{ "
		."\"comment_id\" : {$comment_id_r[$i]} , "
		."\"comment_user\" : \"{$comment_user_id_r[$i]}\", "
		."\"comment_text\" : \"".rawurlencode($comment_text_r[$i])."\", "
		."\"comment_create\" : \"{$comment_create_date_r[$i]}\", "
		."\"comment_user_name\" : \"".rawurlencode($comment_user_name_r[$i])."\", "
		."\"comment_rank\" : \"{$comment_rank_r[$i]}\", "
		."\"comment_rank_string\" : \"".rawurlencode($comment_rank_string_r[$i])."\", "
		."\"comment_like\" : \"{$comment_like_r[$i]}\", "
		."\"comment_current_user_like\" : \"{$comment_current_user_like_r[$i]}\" "
		."}";
		if($jsonStr != ""){
			$jsonStr .= ", ".$item;
		}else{
			$jsonStr .= $item;
		}
	}
	$jsonStr = "{ \"total\" : {$total_count}, \"page_index\" : ".$page.", \"page_count\" : ".ceil($total_count/$pageSize).", \"comments\" :[ {$jsonStr} ]}";
	echo $jsonStr;
	
}
$mysqli->close();
?>